//
//  ZFActivityView.m
//  components
//
//  Created by fyzq on 2021/7/13.
//

#import "ZFActivityView.h"
#import "config.h"
#import "UIView+category.h"

@interface ZFActivityView()
@property(nonatomic,strong)ZFBackGroundView *backgroundView;
@property(nonatomic,strong)ZFBackGroundView *contentView;
@end

@implementation ZFActivityView
- (instancetype)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self commonInit];
    }
    return self;
}

- (id)initWithView:(UIView *)view {
    NSAssert(view, @"View must not be nil.");
    return [self initWithFrame:view.bounds];
}
-(void)commonInit{
    _animationType = ActivityAnimationFade;
    _duration = 0.3;
    [self setupViews];
}
-(void)setupViews{
 
    ZFBackGroundView *backgroundView = [[ZFBackGroundView alloc] initWithFrame:self.bounds];
    backgroundView.backgroundColor = [UIColor clearColor];
    backgroundView.alpha = 0.f;
    [self addSubview:backgroundView];
    self.backgroundView = backgroundView;
    
    ZFBackGroundView *contentView = [[ZFBackGroundView alloc] initWithFrame:self.bounds];
    contentView.alpha = 0.f;
    [backgroundView addSubview:contentView];
    self.contentView = contentView;
    
    [self uplayout];
    ZFWeakSelf;
    [backgroundView addTarget:^(UIGestureRecognizer * _Nonnull x) {
        ZFStrongSelf;
        if (strongSelf.canTouchBackground) {
            [strongSelf hideAnimated:YES];
        }
    }];
}


-(void)uplayout{
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[contentView]-40-|" options:0 metrics:0 views:@{@"contentView":self.contentView}]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[contentView(>=200)]" options:0 metrics:0 views:@{@"contentView":self.contentView}]];
    
}


///开始动画
-(void)showAnimated:(BOOL)animated{
    if (animated) {
        [self animatingIn:YES withType:self.animationType completion:^(BOOL finished) {
            
        }];
    }else{
        self.contentView.alpha=1.0f;
        self.backgroundView.alpha =1.0f;
    }
}
///结束动画
-(void)hideAnimated:(BOOL)animated{
    if (animated) {
        ZFWeakSelf;
        [self animatingIn:NO withType:self.animationType completion:^(BOOL finished) {
            ZFStrongSelf;
            [strongSelf done];
        }];
    }else{
        [self done];
    }
}
///延迟多久结束
-(void)hideAnimated:(BOOL)animated delay:(NSTimeInterval)delay{
    ZFWeakSelf;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        ZFStrongSelf;
        [strongSelf hideAnimated:animated];
     
    });
}
//完成
-(void)done{
    self.alpha = 0.0f;
    self.contentView.alpha = 0.f;
    self.backgroundView.alpha =0.f;
    if (self.needRemoveSuperView) {
        [self removeFromSuperview];
    }
}

-(void)animatingIn:(BOOL)animatingIn withType:(ActivityAnimation)type  completion:(void(^)(BOOL finished))completion{
    if (type == ActivityAnimationZoom) {
        type = animatingIn ? ActivityAnimationZoomIn : ActivityAnimationZoomOut;
    }

    CGAffineTransform small = CGAffineTransformMakeScale(0.5f, 0.5f);
    CGAffineTransform large = CGAffineTransformMakeScale(1.5f, 1.5f);
    
    if(animatingIn && self.contentView.alpha==0.f && type == ActivityAnimationZoomIn){
        self.contentView.transform = small;
    }else if(animatingIn && self.contentView.alpha==0.f && type == ActivityAnimationZoomOut){
        self.contentView.transform = large;
    }
    
    
    ZFWeakSelf;
    [UIView animateWithDuration:_duration delay:0 usingSpringWithDamping:1.0f initialSpringVelocity:0.f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        ZFStrongSelf;
        if(animatingIn){
            strongSelf.contentView.transform = CGAffineTransformIdentity;
        } else if (!animatingIn && type == ActivityAnimationZoomIn) {
            strongSelf.contentView.transform = large;
        } else if (!animatingIn && type == ActivityAnimationZoomOut) {
            strongSelf.contentView.transform = small;
        }
        CGFloat alpha = animatingIn ? 1.f : 0.f;
        strongSelf.contentView.alpha = alpha;
        strongSelf.backgroundView.alpha = alpha;
    } completion:completion];
}

@end

@implementation ZFBackGroundView

#pragma mark --- setter
- (void)setGroundImage:(UIImage *)groundImage{
    _groundImage = groundImage;
    self.layer.contents = (id)groundImage.CGImage;
}

@end
